#include "DDCPMessage.hpp"
#include "SipMessageCommon.hpp"

namespace sips{}
using namespace sips;
typedef CDDCPItemTypes CTypes;

std::string ConvetString( int Num )
{
    char Temp[32];
    std::string str;
    sprintf(Temp,"%d",Num);
    str  = Temp;
    return str;
}


int main(int argc, char* argv[]){

    std::string CatalogXMl;
    SCatalog Catalog;
    SSub wkSub;

    wkSub.szAddress = "123123";
    wkSub.szPrivilege = "1";

    Catalog.szParent = "123";
    Catalog.iSubNum = 1;
    Catalog.SubList.push_back(wkSub);

    CDDCPMessage cm(CatalogXMl.c_str());
    if (Catalog.iResult < 0)
    {
        cm.itemDo(CTypes::Action).itemDo(CTypes::Variable).setText(GetVariableName(Catalog));
        CatalogXMl = cm.cStr();

        std::cout << CatalogXMl;
        getchar();

        if (!Catalog.szParent.empty()){
            cm.itemDo(CTypes::Action).itemDo(CTypes::Parent).setText(Catalog.szParent.c_str());
        }
        if (Catalog.iSubNum >= 0){
            cm.itemDo(CTypes::Action).itemDo(CTypes::SubNum).setText(ConvetString(Catalog.iSubNum).c_str());
        }
        std::list<SSub>::const_iterator it = Catalog.SubList.begin();
        for (; it != Catalog.SubList.end(); it++)
        {
            const SSub& item = (*it);
            CDDCPItemHandle& itHandle = cm.itemDo(CTypes::Action).itemDo(CTypes::SubList).addItem(CTypes::Item);
            if (!item.szName.empty())
            {
                itHandle.itemDo(CTypes::Name).setText(item.szName.c_str());
            }
            if (!item.szAddress.empty())
            {
                itHandle.itemDo(CTypes::Address).setText(item.szAddress.c_str());
            }
            if (!item.szPrivilege.empty())
            {
                itHandle.itemDo(CTypes::Privilege).setText(item.szPrivilege.c_str());
            }
            if (!item.szStatus.empty())
            {
                itHandle.itemDo(CTypes::Status).setText(item.szStatus.c_str());
            }
            if (!item.szLongitude.empty())
            {
                itHandle.itemDo(CTypes::Longitude).setText(item.szLongitude.c_str());
            }
            if (!item.szLatitude.empty())
            {
                itHandle.itemDo(CTypes::Latitude).setText(item.szLatitude.c_str());
            }
            if (!item.szElevation.empty())
            {
                itHandle.itemDo(CTypes::Elevation).setText(item.szElevation.c_str());
            }
            if (!item.szRoadway.empty())
            {
                itHandle.itemDo(CTypes::Roadway).setText(item.szRoadway.c_str());
            }
            if (!item.szPileNo.empty())
            {
                itHandle.itemDo(CTypes::PileNo).setText(item.szPileNo.c_str());
            }
            if (!item.szDecoderTag.empty())
            {
                itHandle.itemDo(CTypes::DecoderTag).setText(item.szDecoderTag.c_str());
            }
            if (!item.szOperateType.empty())
            {
                itHandle.itemDo(CTypes::OperateType).setText(item.szOperateType.c_str());
            }
            if (!item.szTotalSpace.empty())
            {
                itHandle.itemDo(CTypes::TotalSpace).setText(item.szTotalSpace.c_str());
            }
            if (item.iMaxRecord >= 0)
            {
                itHandle.itemDo(CTypes::MaxRecord).setText(ConvetString(item.iMaxRecord).c_str());
            }
            if (!item.szFreeSpace.empty())
            {
                itHandle.itemDo(CTypes::FreeSpace).setText(item.szFreeSpace.c_str());
            }
            if (item.iCurrentRecord >= 0)
            {
                itHandle.itemDo(CTypes::CurrentRecord).setText(ConvetString(item.iCurrentRecord).c_str());
            }
            if (item.iCurrentVOD >= 0)
            {
                itHandle.itemDo(CTypes::CurrentVOD).setText(ConvetString(item.iCurrentVOD).c_str());
            }
            std::list<std::string>::const_iterator itSub = item.CameraAddressList.begin();
            for (; itSub != item.CameraAddressList.end(); itSub++)
            {
                itHandle.addItem(CTypes::CameraAddress).setText((*itSub).c_str());
            }
        }
    }
    else
    {
        cm.itemDo(CTypes::Response).itemDo(CTypes::Variable).setText(GetVariableName(Catalog));
        cm.itemDo(CTypes::Response).itemDo(CTypes::Result).setText(ConvetString(Catalog.iResult).c_str());
    }
    CatalogXMl = cm.cStr();

    std::cout << CatalogXMl;
    getchar();

    return 0;
};